import 'package:bilibili/navigator/bottom_navigator.dart';
import 'package:bilibili/page/home_page.dart';
import 'package:bilibili/page/login_page.dart';
import 'package:bilibili/page/registration_page.dart';
import 'package:bilibili/page/video_detail_page.dart';
import 'package:flutter/material.dart';

typedef RouteChangeListener = Function(
    RouteStatusInfo current, RouteStatusInfo pre);

pageWrap(Widget child) {
  return MaterialPage(key: ValueKey(child.hashCode), child: child);
}

/// 获取 routeStatus 在页面栈中的位置
int getPageIndex(List<MaterialPage> pages, RouteStatus routeStatus) {
  for (int i = 0; i < pages.length; i++) {
    MaterialPage page = pages[i];
    if (getStatus(page.child) == routeStatus) {
      return i;
    }
  }

  return -1;
}

/// 自定义路由封装，路由状态
enum RouteStatus { login, registration, home, detail, unknown }

/// 获取page对应的RouteStatus
RouteStatus getStatus(Widget page) {
  if (page is LoginPage) {
    return RouteStatus.login;
  } else if (page is RegistrationPage) {
    return RouteStatus.registration;
  } else if (page is BottomNavigator) {
    return RouteStatus.home;
  } else if (page is VideoDetailPage) {
    return RouteStatus.detail;
  } else {
    return RouteStatus.unknown;
  }
}

/// 路由信息
class RouteStatusInfo {
  final RouteStatus routeStatus;
  final Widget page;

  RouteStatusInfo(this.routeStatus, this.page);
}

class HiNavigator extends _RouteJumpListener {
  HiNavigator._();

  static HiNavigator? _instance;
  static HiNavigator getInstance() {
    _instance ??= HiNavigator._();

    return _instance!;
  }

  RouteStatusInfo? _bottomTab;
  void onBottomTabChange(int index, Widget page) {
    _bottomTab = RouteStatusInfo(getStatus(page), page);
    _notify(_bottomTab!);
  }

  // 路由跳转
  RouteJumpListener? _routeJump;
  void registerRouteJump(RouteJumpListener routeJmumpListener) {
    _routeJump = routeJmumpListener;
  }

  @override
  void onJumpTo(RouteStatus reouteState, {Map? args}) {
    if (_routeJump != null && _routeJump!.onJumpTo != null) {
      _routeJump!.onJumpTo!(reouteState, args: args);
    }
  }

  // 路由监听逻辑，用于处理压后台或进前台事件的分发
  RouteStatusInfo? _current;
  List<RouteChangeListener> _listeners = [];
  // 添加
  void addListener(RouteChangeListener listener) {
    if (!_listeners.contains(listener)) {
      _listeners.add(listener);
    }
  }

  // 删除
  void removeListener(RouteChangeListener listener) {
    _listeners.remove(listener);
  }

  // 分发
  void notifyListener(
      List<MaterialPage> currentPages, List<MaterialPage> prePages) {
    if (currentPages == prePages) return;
    var current = RouteStatusInfo(
        getStatus(currentPages.last.child), currentPages.last.child);
    _notify(current);
  }

  void _notify(RouteStatusInfo current) {
    print('hi_navigator:current:${current.page}');
    print('hi_navigator:pre:${_current?.page}');

    for (var listener in _listeners) {
      listener(current, _current!);
    }

    _current = current;
  }
}

abstract class _RouteJumpListener {
  void onJumpTo(RouteStatus reouteState, {Map args});
}

typedef OnJumpTo = void Function(RouteStatus routeStatus, {Map? args});

class RouteJumpListener {
  OnJumpTo? onJumpTo;
  RouteJumpListener({this.onJumpTo});
}
